/*
 * Copyright (c) 2025 HiSilicon (Shanghai) Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SS_AF_MANAGER_H
#define SS_AF_MANAGER_H

#include <stdint.h>
#include <stdbool.h>
#include "ot_type.h"
#include "ot_af_common.h"
#include "ot_af_tuning_info.h"
#include "ss_scene_detection.h"
#include "ss_pdaf.h"
#include "ss_cdaf.h"
#include "ss_daaf.h"
#include "ot_motor.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

typedef enum {
    OT_ERR_AF_OK = 0,
    OT_ERR_AF_NOT_INIT,             /* 未初始化 */
    OT_ERR_AF_INVALID_PIPE_ID,      /* pipe号错误 */
    OT_ERR_AF_ILLEGAL_HANDLE,       /* 非法的handle值 */
    OT_ERR_AF_ILLEGAL_PARAM,        /* 参数非法 */
    OT_ERR_AF_MEM_MALLOC_FAILED,    /* 内存分配失败 */
    OT_ERR_AF_NULL_PTR,             /* 指针为空 */
    OT_ERR_AF_MODE_NOT_SUPPORT,     /* 对焦模式不支持 */
    OT_ERR_AF_NOT_START,            /* 自动对焦未启动 */
    OT_ERR_AF_FAILURE               /* 失败 */
} OT_AF_ErrorCode;

typedef enum {
    OT_AF_MODE_SAF = 0, /* AF AUTO Single */
    OT_AF_MODE_CAF,     /* AF continue */
    OT_AF_MODE_MAF,     /* manual focus */
    OT_AF_MODE_BUTT
} OT_AF_Mode;

typedef enum {
    OT_AF_ERROR_TYPE_FOCUS_FAILED = 0,
    OT_AF_ERROR_TYPE_BUTT
} OT_AF_ErrorEventType;

typedef enum {
    OT_AF_INFO_TYPE_STATE_FOCUSED = 0, /* 对焦成功 更新合焦区域对焦框 */
    OT_AF_INFO_TYPE_STATE_FOCUS_INFO, /* 对焦信息 */
    OT_AF_INFO_TYPE_PDAF_INVALID, /* PD不可用，下一步会切到CDAF粗搜 上报信息，客户可以锁焦 */
    OT_AF_INFO_TYPE_BUTT
} OT_AF_InfoEventType;

typedef struct {
    /* 窗口配置 */
    OT_AF_WindowConfig winConfig;
    OT_PDAF_Results pdafResults;
    OT_AF_CDAF_FvInfoResult fvInfoResults;
    OT_AF_FocusWindow focusWin;
} OT_AF_FocusInfo;

/**
 * @brief Defines listener callbacks for the focus.
 */
typedef struct {
    /** Private data handle */
    td_void *context;

    /**
     * @brief Called when a error occurs during focus. This callback is used to report the errors.
     *
     * @param context Indicates the private data handle.
     * @param errorType Indicates the error type. For details, see {@link OT_AF_ErrorEventType}.
     * @param errorCode Indicates the error code.
     * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
     * @since 1.0
     * @version 1.0
     */
    td_s32 (*onError)(td_void *context, OT_AF_ErrorEventType errorType, td_u8 *data, td_u32 len);

    /**
     * @brief Called when an information event occurs during focus.
     * This callback is used to report focus information.
     *
     * @param context Indicates the private data handle.
     * @param type Indicates the information type. For details, see {@link OT_AF_InfoEventType}.
     * @param extra Indicates other information.
     * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
     * @since 1.0
     * @version 1.0
     */
    td_s32 (*onInfo)(td_void *context, OT_AF_InfoEventType infoType, td_u8 *data, td_u32 len);
} OT_AF_Callback;


typedef enum {
    OT_AF_STATE_INACTIVE,               /* 初始状态，未启动对焦，或者reset，no lens movement */
    OT_AF_STATE_PDAF_CONVERGE,          /* 相位差对焦 收敛中 */
    OT_AF_STATE_DAAF_CONVERGE,          /* 深度辅助对焦 收敛中 */
    OT_AF_STATE_CDAF_FULL_SWEEP,        /* 对比度对焦，爬坡搜索中 */
    OT_AF_STATE_CDAF_PARALLEL_SEARCH,   /* PDAF 不可信情况下 CDAF并行搜索中 */
    OT_AF_STATE_CDAF_FINE_SEARCH,       /* PDAF或 DAAF 搜索结束 CDAF 细搜过程中  */
    OT_AF_STATE_ZOOM_TRACKING,          /* 变倍跟随中 */
    OT_AF_STATE_INFOCUS,                /* in focus */
    OT_AF_STATE_FOCUS_FAILED,           /* 对焦失败 */
    OT_AF_STATE_BUTT                    /* 无效状态 */
} OT_AF_State;

typedef enum {
    OT_AF_CMD_ID_QURYE_AF_STATE = 0,
    OT_AF_CMD_ID_BUTT
} OT_AF_CmdId;

td_s32 SS_AF_Init(td_void **handle, const OT_AF_InitParam *initParam);
td_s32 SS_AF_Deinit(td_void *handle);

td_s32 SS_AF_SetDynamicTuning(td_void *handle, OT_AF_DynamicTuningInfo dynamicTuning);
td_s32 SS_AF_Start(td_void *handle);
td_s32 SS_AF_Stop(td_void *handle);

td_s32 SS_AF_Lock(td_void *handle);
td_s32 SS_AF_UnLock(td_void *handle);

td_s32 SS_AF_Trigger(td_void *handle);

td_s32 SS_AF_EnableExhibitMode(td_void *handle, td_bool enable);
td_s32 SS_AF_GetWindowCapability(td_void *handle, OT_AF_WindowCapability *cap);

td_s32 SS_AF_SetFocusWindow(td_void *handle, OT_AF_WindowConfig *config);
td_s32 SS_AF_GetFocusWindow(td_void *handle, OT_AF_WindowConfig *winConfig);


td_s32 SS_AF_SetMode(td_void *handle, OT_AF_Mode mode);
td_s32 SS_AF_GetMode(td_void *handle, OT_AF_Mode *mode);

td_s32 SS_AF_SetZoomInfo(td_void *handle, const OT_AF_ZoomInfo *info);
td_s32 SS_AF_GetCurrentZoomInfo(td_void *handle, OT_AF_ZoomInfo *info);
td_s32 SS_AF_GetMaxZoomRatio(td_void *handle, OT_AF_ZoomType zoomType, td_float *maxZoomRatio);

td_s32 SS_AF_RegisterFocusMotorAlg(td_void *handle, OT_AF_FocusMotorAlgIntf *obj);
td_s32 SS_AF_RegisterZoomMotorAlg(td_void *handle, OT_AF_ZoomMotorAlgIntf *obj);

td_s32 SS_AF_RegisterCDAFAlg(td_void *handle, OT_AF_CDAFAlgIntf *obj);
td_s32 SS_AF_RegisterPDAFAlg(td_void *handle, OT_AF_PDAFAlgIntf *obj,
    const OT_PDAF_InitParam *pdInitParams);
td_s32 SS_AF_RegisterDAAFAlg(td_void *handle, OT_AF_DAAFAlgIntf *obj);

td_s32 SS_AF_RegisterSceneDetectionAlg(td_void *handle, OT_AF_SceneDetectionIntf *obj);

td_s32 SS_AF_RegisterCb(td_void *handle, OT_AF_Callback *cb);
td_s32 SS_AF_Invoke(td_void *handle, OT_AF_CmdId cmdId, td_void *data, td_u32 len);

td_s32 SS_AF_SetManualFocusDistance(td_void *handle, td_float focusDistance);
td_s32 SS_AF_SetFocusMotorSpeed(td_void *handle, td_s32 speed);
td_s32 SS_AF_GetFocusMotorSpeed(td_void *handle, td_s32 *speed);
td_s32 SS_AF_SetZoomMotorSpeed(td_void *handle, td_s32 speed);
td_s32 SS_AF_GetZoomMotorSpeed(td_void *handle, td_s32 *speed);

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

#endif
